home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: in2.uu.net!world!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Re: Calling a Function Twice from a printf statement.
- Message-ID: <DnrALq.758@mv.mv.com>
- Mime-Version: 1.0
- Organization: GSSI
- Date: Mon, 4 Mar 1996 18:41:50 GMT
- References: <4he6q9$6r6@newsbf02.news.aol.com>
- X-Newsreader: WinVN 0.93.10
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <4he6q9$6r6@newsbf02.news.aol.com>, razine@aol.com says...
- >
- >I am trying to call a function twice from my program on the same printf
- >line and have run into a small problem. i was wondering if someone can
- >explain how to fix it.
- >
- >
- >void main(void) {
- >
- > printf("First Number is %d , the Second s %d \n",num(0),num(1));
- >
- >}
- >
- >int num(int index) {
- > int return_number;
- >
- > switch(index) {
- > case 0 : return_number=0;
- > case 1 : return_number=1;
- > }
- > return(return_number);
- >}
- >
- >
- >Now with this function, the printf line will display 0 for the first one
- >and garbage for the second one.. I then thought if I made the variable
- >return_number a static variable it would have fixed it but then when i
- >made the change the printf line would give me zero for both of them. Any
- >ideas?
-
- Your program as you posted it will print "1" for both values because your
- forget break statement after assigning "0" to return_number - it will
- immediately reassigne "1" to this variable.
- Also - it is a bad practice to use "void main()" because it is not supported
- by standard (thow most compillers will accept it).
-
-
- >
- >Darrell
- >
-
- --
- <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-